<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.name"
              clearable
              @clear="getMenuList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getMenuList"
              ></el-button>
            </el-input></div
        ></el-col>

        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addMenuDialogVisible = true"
              >新增菜单</el-button
            >
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="success" @click="getMenuList">刷新</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 菜单列表区域 -->
      <el-table border stripe width="100%" :data="menuList">
        <el-table-column fixed prop="id" label="序号"> </el-table-column>
        <el-table-column fixed prop="note" label="菜单名" width="100">
        </el-table-column>
        <el-table-column fixed prop="name" label="菜单标识"> </el-table-column>

        <el-table-column fixed prop="parentId" label="父菜单编号">
        </el-table-column>
        <el-table-column fixed prop="icon" label="图标"> </el-table-column>
        <el-table-column fixed prop="path" label="路径"> </el-table-column>
        <el-table-column fixed prop="depth" label="深度"> </el-table-column>
        <el-table-column fixed label="操作" width="280">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="编辑"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-edit"
                circle
                @click="editDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeDialog(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑菜单详情对话框 -->
    <el-dialog
      title=" 编辑菜单详细信息"
      :visible.sync="editMenuDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form :model="editMenuForm" label-width="100px">
        <el-form-item label="图标">
          <el-input v-model="editMenuForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="父节点">
          <el-input v-model="editMenuForm.parentId"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="editMenuForm.path"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editMenuDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onEditMenu">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加菜单对话框 -->
    <el-dialog
      title="添加菜单"
      :visible.sync="addMenuDialogVisible"
      width="50%"
      @close="addMenuDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addMenuForm"
        :rules="addMenuFormRules"
        ref="addMenuRef"
        label-width="100px"
      >
        <el-form-item label="菜单名" prop="note">
          <el-input v-model="addMenuForm.note"></el-input>
        </el-form-item>
        <el-form-item label="菜单标识" prop="name">
          <el-input v-model="addMenuForm.name"></el-input>
        </el-form-item>

        <el-form-item label="图标" prop="icon">
          <el-input v-model="addMenuForm.icon"></el-input
        ></el-form-item>
        <el-form-item label="路径" prop="path">
          <el-input v-model="addMenuForm.path"></el-input>
        </el-form-item>
        <el-form-item label="父节点编号" prop="parentId">
          <el-input v-model="addMenuForm.parentId"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMenuDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSaveMenu">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除目录对话框 -->
    <el-dialog
      title="删除目录"
      :visible.sync="removeMenuDialogVisible"
      width="30%"
      center
    >
      <span>确定删除掉:{{ removeMenuForm.note }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeMenuDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onRemoveMenu">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMenuById,
  pageMenu,
  saveMenu,
  updateMenu,
  removeMenuById,
} from "@/api/auth.js";
export default {
  data() {
    return {
      //获取菜单列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      menuList: [],
      total: 0,

      //控制编辑菜单对话框的显示与隐藏
      editMenuDialogVisible: false,
      //编辑菜单对象
      editMenuForm: {
        icon: "",
        path: "",
        parentId: null,
      },

      //添加菜单对话框的显示与隐藏
      addMenuDialogVisible: false,
      addMenuForm: {
        icon: "",
        name: "",
        note: "",
        path: "",
        parentId: null,
      },
      addMenuFormRules: {
        icon: [
          {
            required: true,
            message: "请输入图标",
            trigger: "blur",
          },
        ],
        note: [
          {
            required: true,
            message: "请输入备注",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
        path: [
          {
            required: true,
            message: "请输入路径",
            trigger: "blur",
          },
        ],
      },
      //执行删除的对象
      removeMenuForm: {},
      //删除的弹窗对话框
      removeMenuDialogVisible: false,
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    async getMenuList() {
      const { data: res } = await pageMenu(
        this.queryInfo.current,
        this.queryInfo.size
      );

      this.menuList = res.data.records;
      this.total = res.data.total;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getMenuList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getMenuList();
    },

    //展示查看菜单详情的对话框
    showDialog(id) {
      getMenu(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showMenuDialogVisible = true;
          this.editMenuForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //新增菜单输入框重置
    addMenuDialogClosed() {
      this.$refs.addMenuRef.resetFields();
    },
    //执行保存
    onSaveMenu() {
      saveMenu(this.addMenuForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.addMenuDialogVisible = false;
          this.getMenuList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //显示出删除弹窗
    removeDialog(id) {
      getMenuById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeMenuForm = res.data.data;
          this.removeMenuDialogVisible = true;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //执行删除菜单操作
    onRemoveMenu() {
      removeMenuById(this.removeMenuForm.id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeMenuDialogVisible = false;
          this.getMenuList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //显示出编辑弹窗
    editDialog(id) {
      getMenuById(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.editMenuForm = res.data.data;
          this.editMenuDialogVisible = true;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //执行修改操作
    onEditMenu() {
      updateMenu(this.editMenuForm.id, this.editMenuForm)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.editMenuDialogVisible = false;
          this.getMenuList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>